package com.project.grenting.controller;

import com.project.grenting.pojo.Customer;
import com.project.grenting.service.customer.CustomerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
@Controller
@RequestMapping("/customer")
public class CustomerController {


    @Resource
    private CustomerService customerService;


    @RequestMapping("/customerList")
    @RequiresPermissions("customerList")
    public String customerList(Model model) {

        List<Customer> customerList = customerService.getAllCustomers();
        model.addAttribute("customerList", customerList);

        return "customer/customerList";
    }


    @RequestMapping("/customerAdd")
    @RequiresPermissions("customerAdd")
    public String customerAdd(Model model) {
        List<Customer> customerList = customerService.getAllCustomers();
        model.addAttribute("customerList", customerList);

        return "customer/customerAdd";
    }

    @RequestMapping("/doCustomerAdd")
    @RequiresPermissions("customerAdd")
    public String doCustomerAdd(Customer customer, Model model) {

        /* Customer customerInfo = (Customer) SecurityUtils.getSubject().getSession().getAttribute("currentCustomer");*/
        /* System.out.println(customerInfo.getId()+"=============================");*/

        System.out.println(customer.toString());

        int success = customerService.addCustomer(customer);
        String msg = "";
        if (success == 1) {
            msg = "添加用户成功";
        } else {
            msg = "添加用户失败";
        }
        model.addAttribute("msg", msg);

        return "forward:/customer/customerAdd";
    }


    @GetMapping("/customerEdit/{id}")
    @RequiresPermissions("customerEdit")
    public String customerEdit(@PathVariable("id") int id, Model model) {
        Customer customer = customerService.getCustomerById(id);
        model.addAttribute("customer", customer);
        return "customer/customerEdit";
    }

    @PostMapping("/doCustomerEdit")
    @RequiresPermissions("customerEdit")
    public String doCustomerEdit(Customer customer, Model model) {

        //暂不实现，记住原则，修改用户之前，需将用户角色中间表根据用户id删除，然后重新分配角色

        return "customer/customerList";
    }
}

